package com.aiyige.page.learn.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.city.model.City;
import com.aiyige.model.moment.entity.MajorCourse;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.model.moment.entity.TrainingCard;
import com.aiyige.model.moment.entity.TrainingCourse;
import com.aiyige.page.detail.DetailPage;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.DateFormatUtil;
import com.aiyige.utils.DummyDataUtil;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.WebImageUrlModifier;
import com.aiyige.utils.widget.RoundCornerImageView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.netease.nim.uikit.common.util.string.StringUtil;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zhuyakun on 2017/12/12.
 */

public class LearnDataAdapter extends BaseQuickAdapter<Moment, BaseViewHolder> {

    public LearnDataAdapter() {
        super(new LinkedList<Moment>());
    }

    @Override
    protected void convert(BaseViewHolder helper, Moment item) {
        switch (item.getSubjectIndex()) {
            case Moment.SUBJECT_VIDEO_COURSE_INDEX:
                ((LearnVideoViewHolder) helper).bindData(item);
                break;
            case Moment.SUBJECT_MAJOR_COURSE_INDEX:
                ((MajorCourseViewHolder) helper).bindData(item);
                break;
            case Moment.SUBJECT_TRAINING_COURSE_INDEX:
                ((TraingClassViewHolder) helper).bindData(item);
                break;
            case Moment.SUBJECT_TRAINING_CARD_INDEX:
                ((TraingCardViewHolder) helper).bindData(item);
                break;
        }
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case Moment.SUBJECT_VIDEO_COURSE_INDEX:
                baseViewHolder = new LearnVideoViewHolder(getItemView(R.layout.learn_video_item, parent));
                break;
            case Moment.SUBJECT_MAJOR_COURSE_INDEX:
                baseViewHolder = new MajorCourseViewHolder(getItemView(R.layout.major_course_item, parent));
                break;
            case Moment.SUBJECT_TRAINING_COURSE_INDEX:
                baseViewHolder = new TraingClassViewHolder(getItemView(R.layout.traing_class_item, parent));
                break;
            case Moment.SUBJECT_TRAINING_CARD_INDEX:
                baseViewHolder = new TraingCardViewHolder(getItemView(R.layout.traing_card_item, parent));
                break;
            default:
                baseViewHolder = new BaseViewHolder(getItemView(R.layout.wild_card_item, parent));
                break;
        }
        return baseViewHolder;
    }

    @Override
    protected int getDefItemViewType(int position) {
        return mData.get(position).getSubjectIndex();
    }

    public class TraingCardViewHolder extends BaseViewHolder {
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.verifyIv)
        ImageView verifyIv;
        @BindView(R.id.userNameTv)
        TextView userNameTv;
        @BindView(R.id.freeTv)
        TextView freeTv;
        @BindView(R.id.priceTv)
        TextView priceTv;
        @BindView(R.id.priceLayout)
        LinearLayout priceLayout;
        @BindView(R.id.priceContainer)
        FrameLayout priceContainer;
        @BindView(R.id.buyNumTv)
        TextView buyNumTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.titleLayout)
        FrameLayout titleLayout;
        @BindView(R.id.cardTypeTv)
        TextView cardTypeTv;
        @BindView(R.id.cardTypeLayout)
        LinearLayout cardTypeLayout;
        @BindView(R.id.effectiveDataTv)
        TextView effectiveDataTv;
        @BindView(R.id.validityTv)
        TextView validityTv;
        @BindView(R.id.validityUnitTv)
        TextView validityUnitTv;
        @BindView(R.id.containerLayout)
        LinearLayout containerLayout;
        Moment record;

        public TraingCardViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(Moment data) {
            record = data;
            GlideUtil.with(itemView.getContext())
                    .loadAvatar(record.getCreatorBackup().getAvatar())
                    .into(avatarIv);
            titleTv.setText(record.getTitle());
            GlideUtil.with(itemView.getContext())
                    .loadCover(ListUtil.isEmpty(record.getCover()) ? "" : record.getCover().get(0))
                    .into(coverIv);
            userNameTv.setText(record.getCreatorBackup().getName());
            priceLayout.setVisibility(View.VISIBLE);
            freeTv.setVisibility(View.INVISIBLE);
            priceTv.setText(StringUtils.priceFormat(data.getCommodityBackup().getPrice()));
            buyNumTv.setText(String.format(StringUtils.getString(R.string.buy_num), StringUtils.formatNum(data.getStatisticsBackup().getSellCount())));
            TrainingCard trainingCard;
            try {
                trainingCard = JSON.parseObject(record.getMoreBackup(), TrainingCard.class);
            } catch (Exception e) {
                trainingCard = null;
            }

            try {
                switch (trainingCard.getCardType()) {
                    case Moment.TRAINING_CARD_TYPE_TIME_CARD:
                        cardTypeTv.setText(R.string.time_card_option);
                        break;
                    case Moment.TRAINING_CARD_TYPE_MEASURED_CARD:
                        cardTypeTv.setText(R.string.count_card_option);
                        break;
                    case Moment.TRAINING_CARD_TYPE_RECHARGEABLE_CARD:
                        cardTypeTv.setText(R.string.prepaid_card_option);
                        break;
                }

            } catch (Exception e) {
                cardTypeTv.setText("");
            }

            try {
                switch (trainingCard.getTakeEffectType()) {
                    case Moment.TRAINING_CARD_TAKE_EFFECT_TYPE_ATTENDANCE:
                        effectiveDataTv.setText(R.string.attendance_effective);
                        break;
                    case Moment.TRAINING_CARD_TAKE_EFFECT_TYPE_IMMEDIATE:
                        effectiveDataTv.setText(R.string.immediate_effective);
                        break;
                }
            } catch (Exception e) {
                effectiveDataTv.setText("");
            }
            try {
                validityTv.setText(String.valueOf(trainingCard.getTakeEffectPeriod()));
            } catch (Exception e) {
                validityTv.setText("");
            }
        }

        @OnClick({R.id.avatarIv, R.id.userNameTv, R.id.containerLayout})
        public void onViewClick(View v) {
            switch (v.getId()) {
                case R.id.avatarIv:
                case R.id.userNameTv:
                    ARouter.getInstance()
                            .build(ARouterConfig.UserPage)
                            .withString("userId", record.getCreatorBackup().getId())
                            .navigation();
                    break;
                case R.id.containerLayout:
                    Router.start(record.getRouter());
                    break;
            }
        }
    }

    public class TraingClassViewHolder extends BaseViewHolder {
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.verifyIv)
        ImageView verifyIv;
        @BindView(R.id.userNameTv)
        TextView userNameTv;
        @BindView(R.id.freeTv)
        TextView freeTv;
        @BindView(R.id.priceTv)
        TextView priceTv;
        @BindView(R.id.priceLayout)
        LinearLayout priceLayout;
        @BindView(R.id.priceContainer)
        FrameLayout priceContainer;
        @BindView(R.id.spacesTv)
        TextView spacesTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.titleLayout)
        FrameLayout titleLayout;
        @BindView(R.id.locationTv)
        TextView locationTv;
        @BindView(R.id.locationLayout)
        LinearLayout locationLayout;
        @BindView(R.id.openClassDateTv)
        TextView openClassDateTv;
        @BindView(R.id.containerLayout)
        LinearLayout containerLayout;
        Moment record;

        public TraingClassViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(Moment data) {
            record = data;
            GlideUtil.with(itemView.getContext())
                    .loadAvatar(record.getCreatorBackup().getAvatar())
                    .into(avatarIv);
            titleTv.setText(record.getTitle());
            GlideUtil.with(itemView.getContext())
                    .loadCover(ListUtil.isEmpty(record.getCover()) ? "" : record.getCover().get(0))
                    .into(coverIv);
            userNameTv.setText(record.getCreatorBackup().getName());
            priceLayout.setVisibility(View.VISIBLE);
            freeTv.setVisibility(View.INVISIBLE);
            priceTv.setText(StringUtils.priceFormat(data.getCommodityBackup().getPrice()));
            spacesTv.setText(String.format(StringUtils.getString(R.string.spaces), String.valueOf(data.getInventoryBackup().getAvailableInventory() == null ? 0 : data.getInventoryBackup().getAvailableInventory())));
            try {
                TrainingCourse trainingCourse = JSON.parseObject(record.getMoreBackup(), TrainingCourse.class);
                openClassDateTv.setText(DateFormatUtil.formatMS("yyyy-MM-dd", trainingCourse.getStartDate()));
            } catch (Exception e) {
                openClassDateTv.setText("");
            }
            try {
                City city = DummyDataUtil.getCityById(record.getCityId());
                locationTv.setText(city.getCity());
            } catch (Exception e) {
                locationTv.setText("");
            }
        }

        @OnClick({R.id.avatarIv, R.id.userNameTv, R.id.containerLayout})
        public void onViewClick(View v) {
            switch (v.getId()) {
                case R.id.avatarIv:
                case R.id.userNameTv:
                    ARouter.getInstance()
                            .build(ARouterConfig.UserPage)
                            .withString("userId", record.getCreatorBackup().getId())
                            .navigation();
                    break;
                case R.id.containerLayout:
                    Router.start(record.getRouter());
                    break;
            }
        }

    }


    public class MajorCourseViewHolder extends BaseViewHolder {
        @BindView(R.id.linePriceContainer)
        View linePriceContainer;
        @BindView(R.id.linePriceTv)
        TextView linePriceTv;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.verifyIv)
        ImageView verifyIv;
        @BindView(R.id.userNameTv)
        TextView userNameTv;
        @BindView(R.id.freeTv)
        TextView freeTv;
        @BindView(R.id.priceTv)
        TextView priceTv;
        @BindView(R.id.priceLayout)
        LinearLayout priceLayout;
        @BindView(R.id.priceContainer)
        FrameLayout priceContainer;
        @BindView(R.id.spacesTv)
        TextView spacesTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.titleLayout)
        FrameLayout titleLayout;
        @BindView(R.id.locationTv)
        TextView locationTv;
        @BindView(R.id.locationLayout)
        LinearLayout locationLayout;
        @BindView(R.id.openClassDateTv)
        TextView openClassDateTv;
        @BindView(R.id.containerLayout)
        LinearLayout containerLayout;
        Moment record;

        public MajorCourseViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(Moment data) {
            record = data;
            GlideUtil.with(itemView.getContext())
                    .loadAvatar(record.getCreatorBackup().getAvatar())
                    .into(avatarIv);
            titleTv.setText(record.getTitle());
            GlideUtil.with(itemView.getContext())
                    .loadCover(ListUtil.isEmpty(record.getCover()) ? "" : record.getCover().get(0))
                    .into(coverIv);
            userNameTv.setText(record.getCreatorBackup().getName());
            priceLayout.setVisibility(View.VISIBLE);
            freeTv.setVisibility(View.INVISIBLE);
            if (record.getOnSalesBackup().hasLinePrice()) {
                linePriceContainer.setVisibility(View.VISIBLE);
                linePriceTv.setText(StringUtils.priceFormat(data.getCommodityBackup().getPrice()));
                priceTv.setText(StringUtils.priceFormat(record.getOnSalesBackup().getPrice()));
            } else {
                linePriceContainer.setVisibility(View.INVISIBLE);
                priceTv.setText(StringUtils.priceFormat(data.getCommodityBackup().getPrice()));
            }
            spacesTv.setText(String.format(StringUtils.getString(R.string.spaces), String.valueOf(data.getInventoryBackup().getAvailableInventory() == null ? 0 : data.getInventoryBackup().getAvailableInventory())));
            try {
                MajorCourse majorCourse = JSON.parseObject(record.getMoreBackup(), MajorCourse.class);
                openClassDateTv.setText(DateFormatUtil.formatMS("yyyy-MM-dd", majorCourse.getStartDate()));
            } catch (Exception e) {
                openClassDateTv.setText("");
            }
            try {
                City city = DummyDataUtil.getCityById(record.getCityId());
                locationTv.setText(city.getCity());
            } catch (Exception e) {
                locationTv.setText("");
            }

        }

        @OnClick({R.id.avatarIv, R.id.userNameTv, R.id.containerLayout})
        public void onViewClick(View v) {
            switch (v.getId()) {
                case R.id.avatarIv:
                case R.id.userNameTv:
                    ARouter.getInstance()
                            .build(ARouterConfig.UserPage)
                            .withString("userId", record.getCreatorBackup().getId())
                            .navigation();
                    break;
                case R.id.containerLayout:
                    Router.start(record.getRouter());
                    break;
            }
        }

    }

    public class LearnVideoViewHolder extends BaseViewHolder {
        @BindView(R.id.linePriceContainer)
        View linePriceContainer;
        @BindView(R.id.linePriceTv)
        TextView linePriceTv;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.verifyIv)
        ImageView verifyIv;
        @BindView(R.id.userNameTv)
        TextView userNameTv;
        @BindView(R.id.freeTv)
        TextView freeTv;
        @BindView(R.id.priceTv)
        TextView priceTv;
        @BindView(R.id.priceLayout)
        LinearLayout priceLayout;
        @BindView(R.id.priceContainer)
        FrameLayout priceContainer;
        @BindView(R.id.buyNumTv)
        TextView buyNumTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.watchNumTv)
        TextView watchNumTv;
        @BindView(R.id.likeIv)
        ImageView likeIv;
        @BindView(R.id.likeTv)
        TextView likeTv;
        @BindView(R.id.commentIv)
        ImageView commentIv;
        @BindView(R.id.commentTv)
        TextView commentTv;
        @BindView(R.id.collectionIv)
        ImageView collectionIv;
        @BindView(R.id.collectionTv)
        TextView collectionTv;
        @BindView(R.id.containerLayout)
        LinearLayout containerLayout;
        Moment record;

        public LearnVideoViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(Moment data) {
            record = data;
            GlideUtil.with(itemView.getContext())
                    .big()
                    .loadCover(ListUtil.isEmpty(data.getCover()) ? "" : data.getCover().get(0))
                    .into(coverIv);
            titleTv.setText(data.getTitle());
            GlideUtil.with(itemView.getContext())
                    .loadAvatar(WebImageUrlModifier.smallImage(data.getCreatorBackup().getAvatar()))
                    .into(avatarIv);
            userNameTv.setText(data.getCreatorBackup().getName());

            priceLayout.setVisibility(View.VISIBLE);
            freeTv.setVisibility(View.INVISIBLE);

            if (record.getOnSalesBackup().hasLinePrice()) {
                linePriceContainer.setVisibility(View.VISIBLE);
                linePriceTv.setText(StringUtils.priceFormat(data.getCommodityBackup().getPrice()));
                priceTv.setText(StringUtils.priceFormat(record.getOnSalesBackup().getPrice()));
            } else {
                linePriceContainer.setVisibility(View.INVISIBLE);
                priceTv.setText(StringUtils.priceFormat(data.getCommodityBackup().getPrice()));
            }
            buyNumTv.setText(String.format(StringUtils.getString(R.string.buy_num), StringUtils.formatNum(data.getStatisticsBackup().getSellCount())));
            watchNumTv.setText(StringUtils.formatNum(record.getStatisticsBackup().getViewCount()));
            likeTv.setText(StringUtils.formatNum(record.getStatisticsBackup().getPraiseCount()));
            collectionTv.setText(StringUtils.formatNum(record.getStatisticsBackup().getFavoriteCount()));
            commentTv.setText(StringUtils.formatNum(record.getStatisticsBackup().getCommentCount()));
        }

        @OnClick({R.id.containerLayout, R.id.avatarIv, R.id.userNameTv})
        public void onViewClick(View v) {
            switch (v.getId()) {
                case R.id.containerLayout:
                    Router.start(record.getRouter());
                    break;
                case R.id.userNameTv:
                case R.id.avatarIv:
                    ARouter.getInstance()
                            .build(ARouterConfig.UserPage)
                            .withString("userId", record.getCreatorBackup().getId())
                            .navigation();
                    break;
            }
        }
    }

    public class AdViewHolder extends BaseViewHolder {
        @BindView(R.id.adIv)
        RoundCornerImageView adIv;
        @BindView(R.id.containerLayout)
        FrameLayout containerLayout;

        public AdViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(Moment data) {
            Glide.with(itemView.getContext())
                    .load(ListUtil.isEmpty(data.getCover()) ? "" : data.getCover().get(0))
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(adIv);
        }

        @OnClick({R.id.containerLayout})
        public void onViewClick(View v) {
            switch (v.getId()) {
                case R.id.containerLayout:

                    break;
            }
        }
    }

}
